#import "JudgeManager.h"
#import "UserManager.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJsonParser.h"
#import "FileManager.h"


@implementation JudgeManager

@synthesize listJudge;
@synthesize nextJokeId;

-(id)init{
    self = [super init];
    if (self) {
        listJudge = [[NSMutableArray alloc] init];
        nextJokeId = -100;
    }
    return self;
}

- (void)httpPreload:(int)count
{
    Log(@"listJudge.count :%d",listJudge.count);
    if (listJudge.count == 0) {
        int jokeid = 0;

        for (int i=0; i < 6; i++) {
            [self httpLoadNextFrom:jokeid];
            if (jokeid == nextJokeId || nextJokeId == -100) {
                break;
            }
            jokeid = nextJokeId;
        }
    }
    Log(@"finish :listJudge.count :%d",listJudge.count);
}

-(void)httpLoadNextFrom:(NSInteger)jokeId
{
    Log(@"start Id: %d",jokeId);
    
    NSString *url = nil;
    if (jokeId == 0) {
        url = [[[NSString alloc] initWithFormat:JUDGE_FIRST_URL] autorelease];
    }else{
        url = [[[NSString alloc] initWithFormat:JUDGE_NO_FIRST_URL(jokeId)] autorelease];
    }
    
    Log(@"JudgeManager <httpLoadNextFrom> url %@",url);
//    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
//    request.userAgentString = @"lxh_app2_100";
//    [request setRequestMethod:@"GET"];
//    [request addRequestHeader:@"COOKIE" value:[NSString stringWithFormat:@"webpy_session_id=%@",[UserManager sharedInstance].sessionId] ];
//    [request setDelegate:self];
//    [request startSynchronous];
    ASIHttpRequset_GET(url, YES);

    
    NSError *error = [request error];
    if(!error){
        // 当以文本形式读取返回内容时用这个方法
        NSString *tResponseStr = [request responseString];
        //      Log(@"JudgeManager <httpGetJudgeFromId> res: %@",tResponseStr);
        SBJsonParser *tJsonParser = [[SBJsonParser alloc] init];
        id tJsonObjects = [tJsonParser objectWithString:tResponseStr];
        [tJsonParser release], tJsonParser = nil;
        if ([tJsonObjects isKindOfClass:[NSDictionary class]]){
            BOOL tSucc = [[(NSDictionary *)tJsonObjects objectForKey:@"success"] boolValue];
            if (tSucc) {
                JudgeModel *tJudge = [[[JudgeModel alloc] init] autorelease];
                tJudge.judgeId = [[(NSDictionary *)tJsonObjects objectForKey:@"Jokeid"] intValue];
                tJudge.judgeTime = [(NSDictionary *)tJsonObjects objectForKey:@"created"];
                tJudge.judgeText = [(NSDictionary *)tJsonObjects objectForKey:@"content"];
                NSString *tJudgeImgUrl = [(NSDictionary *)tJsonObjects objectForKey:@"uri"];
                if([tJudgeImgUrl hasPrefix:@"http"]||tJudgeImgUrl.length==0){
                    tJudge.judgeImgUrl = tJudgeImgUrl;
                }else{
                    tJudge.judgeImgUrl = [NSString stringWithFormat:@"http://lengxiaohua.com%@",tJudgeImgUrl];
                }
                nextJokeId = tJudge.judgeId;//把当前id传到接口获取下一个笑话
                Log(@"load next from :%d",tJudge.judgeId);
                [listJudge addObject:tJudge];
                Log(@"got next:%d count:%d",nextJokeId,listJudge.count);
            } else {
                Log ( @"JudgeManager <httpLoadNextFrom> failed: success is false");
            }
        }else{
            Log ( @"JudgeManager <httpLoadNextFrom> failed: response parse failed");
        }
    }else{
        Log(@"JudgeManager <httpLoadNextFrom> failed: %@",[error description]);
    }
}

//四个按钮的点击后的结果发送到服务器的方法
-(void)httpJudge:(NSInteger)JudgeId opinion:(NSString *)opinion content:(NSString *)content
{
    Log(@"JudgeManager <httpJudge> id: %d",JudgeId);
    NSString *url =[[NSString alloc]initWithFormat:JUDGE_ACTION(JudgeId, opinion, content)];
//    NSString *url = [[NSString alloc] initWithFormat:@"http://lengxiaohua.com/lengxiaohuaapi/judge?action=judge&Jokeid=%d&opinion=%@&callback=jsonp1357466008684",JudgeId,opinion];
    Log(@"JudgeManager <httpJudge> getting url: %@",url);
//    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
//    request.userAgentString = @"lxh_app2_100";
//    [request setRequestMethod:@"GET"];
//    [request setAllowCompressedResponse:YES];
//    [request addRequestHeader:@"COOKIE" value:[NSString stringWithFormat:@"webpy_session_id=%@",[UserManager sharedInstance].sessionId] ];
//    [request setDelegate:self];
//    [request startSynchronous];
    ASIHttpRequset_GET(url, YES);

    NSError *error = [request error];
    if(!error){
        // 当以文本形式读取返回内容时用这个方法
        NSString *tResponseStr = [request responseString];
        Log(@"JudgeManager <httpJudge> res: %@",tResponseStr);
    }else{
        Log(@"JudgeManager <httpJudge> failed: %@",[error description]);
    }
    [url release];
}

-(void)clearJudgeData{

}

@end
